#include <isa.h>
//#include <monitor/difftest.h>
#include "local-include/reg.h"
#include "local-include/csr.h"
//#include "local-include/intr.h"

const char *regsl[] = {
  "在《RISC-V-Reader-Chinese-v2p1.pdf》搜索\n$0硬编码 0", "ra return address返回地址", "sp stack pointer栈指针", "gp global pointer全局指针", "tp thread pointer线程指针", "t0 temp临时寄存器/alternate link register备用链接寄存器", "t1临时寄存器", "t2临时寄存器",
  "s0saved register保存寄存器/fp frame pointer帧指针", "s1保存寄存器", " \na0 function arguments函数参数/return values返回值", "a1 function arguments函数参数/return values返回值", "a2函数参数", "a3函数参数", "a4函数参数", "a5函数参数", "a6函数参数", "a7函数参数", "\ns2保存寄存器", "s3保存寄存器", "s4保存寄存器", "s5保存寄存器", "s6 保存寄存器", "s7 保存寄存器",
  "s8保存寄存器", "s9保存寄存器", "s10保存寄存器", "s11保存寄存器", "t3临时寄存器", "t4临时寄存器", "t5 临时寄存器", "t6 临时寄存器"
};

const char *fpregsl[] = {
  "ft0浮点临时寄存器", "ft1浮点临时寄存器", "ft2 浮点临时寄存器", "ft3 浮点临时寄存器", "ft4浮点临时寄存器", "ft5浮点临时寄存器", "ft6 浮点临时寄存器", "ft7 浮点临时寄存器",
  "fs0浮点保存寄存器", "fs1浮点保存寄存器", "fa0浮点参数/返回值", "fa1浮点参数/返回值", "fa2浮点参数", "fa3浮点参数", "fa4浮点参数", "fa5浮点参数",
  "fa6浮点参数", "fa7浮点参数", "            fs2 浮点保存寄存器", "fs3 浮点保存寄存器", "fs4浮点保存寄存器", "fs5浮点保存寄存器", "fs6 浮点保存寄存器", "fs7 浮点保存寄存器",
  "fs8浮点保存寄存器", "fs9浮点保存寄存器", "fs10浮点保存寄存器", "fs11浮点保存寄存器", "ft8浮点临时寄存器", "ft9浮点临时寄存器", "ft10浮点临时寄存器", "ft11浮点临时寄存器"
};

void isa_reg_display() {
  int i;
  for (i = 0; i < 32; i ++) {
    printf("%4s: " FMT_WORD " ", regsl[i], cpu.gpr[i]._64);
    if (i % 4 == 3) {
      printf("\n");
    }
  }
  for (i = 0; i < 32; i ++) {
    printf("%4s: " FMT_WORD " ", fpregsl[i], cpu.fpr[i]._64);
    if (i % 4 == 3) {
      printf("\n");
    }
  }
  printf("pc(program counter)程序计数器: " FMT_WORD " mstatus(Machine Status)全局中断使能，以及许多其他的状态: " FMT_WORD " mcause(Machine Exception Cause)发生异常的种类: " FMT_WORD " \nmepc(Machine Exception PC)发生异常的指令: " FMT_WORD "\n",
      cpu.pc, mstatus->val, mcause->val, mepc->val);
  printf("监管者模式(supervisor mode、S模式、S-mode、S mode)有几个异常处理CSR(控制状态寄存器):sepc、stvec、scause、sscratch、stval和sstatus，它们执行与《RISC-V-Reader-Chinese-v2p1.pdf》10.2中描述的机器模式(machine mode、M模式，M-mode、M mode)CSR相同的功能\n");
  printf("%22s \nsstatus(Supervisor Status)全局中断使能，以及许多其他的状态: " FMT_WORD " scause(Supervisor Exception Cause)发生异常的种类: " FMT_WORD " sepc(Supervisor Exception PC)发生异常的指令: " FMT_WORD "\n",
      "", csrid_read(0x100), scause->val, sepc->val);
  printf("satp(Supervisor Address Translation and Protection，监管者地址转换和保护，S模式控制状态寄存器中的一个) 控制了分页系统，通常M模式的程序在第一次进入S模式之前会把零写入 satp以禁用分页，然后S模式的程序在初始化页表以后会再次进行satp寄存器的写操作: " FMT_WORD "\n", satp->val);
  printf("mip(Machine Interrupt Pending)目前正准备处理的中断: " FMT_WORD " mie(Machine Interrupt Enable)处理器目前能处理和必须忽略的中断: " FMT_WORD " mscratch(Machine Scratch)暂时存放一个字大小的数据: " FMT_WORD " sscratch(Supervisor Scratch)暂时存放一个字大小的数据: " FMT_WORD "\n", 
      mip->val, mie->val, mscratch->val, sscratch->val);
  printf("mideleg(Machine Interrupt Delegation，机器中断委托)控制将哪些中断委托给S模式: " FMT_WORD " medeleg同步异常委托给S模式: " FMT_WORD "\n", 
      mideleg->val, medeleg->val);
  printf("mtval(Machine    Trap Value)trap【陷入、自陷】的附加信息:地址例外中出错的地址、发生非法指令例外的指令本身，对于其他异常，它的值为0: " FMT_WORD " \nstval(Supervisor Trap Value)trap【陷入、自陷】的附加信息:地址例外中出错的地址、发生非法指令例外的指令本身，对于其他异常，它的值为0: " FMT_WORD " \nmtvec(Machine    Trap Vector)发生异常时处理器需要跳转到的地址: " FMT_WORD " \nstvec(Supervisor Trap Vector)发生异常时处理器需要跳转到的地址: " FMT_WORD "\n", 
      mtval->val, stval->val, mtvec->val, stvec->val);
#ifdef CONFIG_RVV_010
  //vector register
  extern const char * vregsl[];
  for(i = 0; i < 32; i ++) {
    printf("%s: ", vregsl[i]);
    printf("0x%016lx_%016lx_%016lx_%016lx  ", 
      cpu.vr[i]._64[3], cpu.vr[i]._64[2], cpu.vr[i]._64[1], cpu.vr[i]._64[0]);
    if(i%2) printf("\n");
  }
  printf("vtype: " FMT_WORD " vstart: " FMT_WORD " vxsat: " FMT_WORD "\n", vtype->val, vstart->val, vxsat->val);
  printf("vxrm: " FMT_WORD " vl: " FMT_WORD "\n", vxrm->val, vl->val);
#endif // CONFIG_RVV_010
  fflush(stdout);
}

rtlreg_t isa_reg_str2val(const char *s, bool *success) {
  int i;
  *success = true;
  for (i = 0; i < 32; i ++) {
    if (strcmp(regsl[i], s) == 0) return reg_l(i);
  }

  if (strcmp("pc", s) == 0) return cpu.pc;

  *success = false;
  return 0;
}
